package com.csyf.manager.service.impl;

import com.csyf.manager.channel.base.ServiceEnum;
import com.csyf.manager.dao.SysTradeTypeDao;
import com.csyf.manager.entity.SysTradeType;
import com.csyf.manager.entity.TradeTypeVo;
import com.csyf.manager.service.SysTradeTypeBiz;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysTradeTypeBizImpl implements SysTradeTypeBiz {

    @Autowired
    private SysTradeTypeDao sysTradeTypeDao;

    @Autowired
    private RedisService redisService;

    /**
     * 查询所有的交易类型
     *
     * @return
     */
    @Override
    public List<SysTradeType> queryAllTradeType() {
        List<SysTradeType> tradeTypes = new ArrayList<>();
        for (ServiceEnum item : ServiceEnum.values()) {
            SysTradeType trade = new SysTradeType();
            trade.setTradeType(item.toString());
            tradeTypes.add(trade);
        }
        return tradeTypes;
    }

    /**
     * 查询该代理商存在的交易类型
     */
    @Override
    public List<SysTradeType> findTradeTypeByMchId(String mchId) {
        SysTradeType sysTradeType = new SysTradeType();
        sysTradeType.setMch_id(mchId);
        sysTradeType.setStatus(1);
        List<SysTradeType> tradeTypes = sysTradeTypeDao.select(sysTradeType);
        return tradeTypes;
    }

    /**
     * 保存某商户的交易类型
     *
     * @param vo
     * @return
     */
    @Override
    public ResultEntity saveTradeType(String channelIds, TradeTypeVo vo) {
        SysTradeType sysTradeType = new SysTradeType();
        sysTradeType.setMch_id(vo.getMchId());
        sysTradeType.setAgentId(vo.getAgentId());
        sysTradeType.setStatus(0);
        //1、移除商户的交易类型
        sysTradeTypeDao.updateByPrimaryKeySelective(sysTradeType);

        //删除缓存中的数据
        redisService.deletekeys("service:" + vo.getMchId() + ":");

        //2、保存商户的交易类型
        if (StringUtils.isNotBlank(channelIds)) {
            String[] channelStr = channelIds.split(",");
            for (String channel : channelStr) {
                sysTradeType.setStatus(1);
                sysTradeType.setTradeType(channel);
                sysTradeTypeDao.insert(sysTradeType);
            }
        }

        return ResultUtli.SUCCESS(0000, "成功", null);
    }

    /**
     *
     * @Description: 设置商户通道费率
     *
     * @auther: casper
     * @date: 2018/10/27 14:56
     * @param: [tradeType]
     * @return: void
     *
     */
    @Override
    public void addTradeType(SysTradeType tradeType) {
        SysTradeType sysTradeType = new SysTradeType();
        sysTradeType.setMch_id(tradeType.getMch_id());
        sysTradeType.setTradeType(tradeType.getTradeType());
//        int count = sysTradeTypeDao.selectCount(sysTradeType);
//        if (count>0){
//            sysTradeTypeDao.delete(sysTradeType);
//        }

        tradeType.setStatus(1);
        sysTradeTypeDao.insert(tradeType);
    }

    /**
     * 判断交易类型是否存在
     * @param tradeType
     * @return
     */
    @Override
    public boolean isExist(SysTradeType tradeType) {
        SysTradeType vo = new SysTradeType();
        vo.setTradeType(tradeType.getTradeType());
        vo.setAgentId(tradeType.getAgentId());
        vo.setMch_id(tradeType.getMch_id());
        List<SysTradeType> select = sysTradeTypeDao.select(vo);
        return select.size()>0?true:false;
    }

    /**
     * 停用代理商通道
     * @param vo
     */
    @Override
    public void stopChannelStatus(SysTradeType vo) {
        vo.setStatus(0);
        sysTradeTypeDao.updateByPrimaryKeySelective(vo);

        //删除缓存中的数据
        redisService.deletekeys("service:" + vo.getMch_id() + ":" + vo.getTradeType());
    }

    /**
     * 启用代理商通道
     * @param vo
     */
    @Override
    public void startChannelStatus(SysTradeType vo) {
        vo.setStatus(1);
        sysTradeTypeDao.updateByPrimaryKeySelective(vo);
    }

    /**
     * 代理商交易类型列表
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @Override
    public JqGridVo queryTradeTypeList(SysTradeType vo, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);

        Example example = new Example(SysTradeType.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "mch_id", vo.getMch_id());
        PageUtils.andEqualTo(criteria, "agentId", vo.getAgentId());
        PageUtils.andEqualTo(criteria, "tradeType", vo.getTradeType());

        List<SysTradeType> select = sysTradeTypeDao.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }

    /**
     * 根据条件查询交易类型
     * @param mchId
     * @param tradeType
     * @return
     */
    @Override
    public SysTradeType queryTradeTypeByCondition(String mchId, String tradeType) {
        SysTradeType sysTradeType = new SysTradeType();
        sysTradeType.setTradeType(tradeType);
        sysTradeType.setMch_id(mchId);

        SysTradeType record = sysTradeTypeDao.selectOne(sysTradeType);
        return record;

    }

    /**
     * 编辑交易类型
     * @param tradeTypeForm
     * @return
     */
    @Override
    public int updateByTradeType(SysTradeType tradeTypeForm) {
        int tag = sysTradeTypeDao.updateByPrimaryKeySelective(tradeTypeForm);
        return tag;
    }


}
